﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using Elkitools.EditorParts.ListSelector.EditorParts;

namespace Elkitools.EditorParts.ListSelector
{
    [ToolboxItemAttribute(false)]
    public class DemoWebPart : WebPart
    {
        /// <summary>
        /// This is mapped to the property name that will be managed by our EditorPart
        /// </summary>
        private string CALENDARS_LISTS_PROPERTYNAME = "SelectedListsUrls";

        //-------------------------------------------------------------------
        #region Properties
        //-------------------------------------------------------------------

        /// <summary>
        /// This is the string that contains (concatenated and separated by ';') the urls of the selected lists
        /// </summary>
        public string SelectedListsUrls { get; set; }

        //-------------------------------------------------------------------
        #endregion Properties
        //-------------------------------------------------------------------


        //-------------------------------------------------------------------
        #region Controls management
        //-------------------------------------------------------------------

        /// <summary>
        /// Layout creation
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // reference style
            LiteralControl cssLink = new LiteralControl("<link rel='stylesheet' type='text/css' href='/_layouts/styles/Elkitools/Elkitools.EditorParts.ListSelector.css'/>");
            this.Controls.Add(cssLink);

            // Add our own panel
            Panel rootPanel = new Panel();
            this.Controls.Add(rootPanel);
            rootPanel.CssClass = "Elkito-EditorParts-ListSelctor-RootPanel";

            // Display the urls
            Label lblSelectedUrls = new Label();
            rootPanel.Controls.Add(lblSelectedUrls);
            if (!string.IsNullOrEmpty(SelectedListsUrls))
            {
                lblSelectedUrls.Text = SelectedListsUrls;
            }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            // Initialize Current editorparts
            List<EditorPart> editors = new List<EditorPart>(1);

            // Add the Calendars Lists selection editorpart
            EditorPart CalendarsEditorPart = new CalendarWebsAndListsEditorPart();
            CalendarsEditorPart.ID = this.ID + "CalendarsListsEditorPart";
            CalendarsEditorPart.Title = "Calendars selection";
            ((CalendarWebsAndListsEditorPart)CalendarsEditorPart).WebPartPropertyToManage = this.GetType().GetProperty(CALENDARS_LISTS_PROPERTYNAME);
            editors.Add(CalendarsEditorPart);

            // Call parent function
            EditorPartCollection original = base.CreateEditorParts();
            return new EditorPartCollection(original, editors);
        }

        //-------------------------------------------------------------------
        #endregion Controls management
        //-------------------------------------------------------------------
    }
}
